package valoskmdev.dao;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import valoskmdev.bean.Issue;
import valoskmdev.bean.Vehicle;

public class IssueDao {

	private IssueDao() {
	}

	public static final int ENTITIES_PER_PAGE = 100;

	public static String insertNew(final PersistenceManager pm, final Vehicle vehicle, Issue issue) {

		vehicle.getIssues().add(issue);
		pm.makePersistent(vehicle);
		pm.makePersistent(issue);

		return vehicle.getId().toString();
	}

	@SuppressWarnings("unchecked")
	public static List<Issue> getIssues(final PersistenceManager pm, final String typeParam) {

		final Query query = pm.newQuery(Issue.class);
		query.setFilter("type == param && isDisabled == false && isPublic == true");
		query.declareParameters("String param");
		query.setOrdering("issueDate desc");
		query.setRange(0, 100);
		return (List<Issue>) query.execute(typeParam.toString());

	}

}